package com.atguigu.schedule.service.impl;

import com.atguigu.schedule.service.SrsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.ResponseEntity;
import java.util.Map;

/**
 * SRS服务实现
 */
@Service
@Slf4j
public class SrsServiceImpl implements SrsService {
    
    private final RestTemplate restTemplate = new RestTemplate();
    
    @Value("${srs.server-url:http://8.130.109.229:1985}")
    private String srsServerUrl;
    
    @Value("${srs.api-path:/api/v1}")
    private String srsApiPath;
    
    @Value("${srs.timeout:5000}")
    private int timeout;
    
    @Override
    public boolean checkSrsStatus() {
        try {
            String url = srsServerUrl + srsApiPath + "/summaries";
            ResponseEntity<Map> response = restTemplate.getForEntity(url, Map.class);
            return response.getStatusCode().is2xxSuccessful();
        } catch (Exception e) {
            log.error("检查SRS服务状态失败: {}", e.getMessage());
            return false;
        }
    }
    
    @Override
    public String getSrsInfo() {
        try {
            String url = srsServerUrl + srsApiPath + "/summaries";
            ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
            return response.getBody();
        } catch (Exception e) {
            log.error("获取SRS服务信息失败: {}", e.getMessage());
            return null;
        }
    }
    
    @Override
    public boolean isStreamExists(String streamName) {
        try {
            String url = srsServerUrl + srsApiPath + "/streams/" + streamName;
            ResponseEntity<Map> response = restTemplate.getForEntity(url, Map.class);
            return response.getStatusCode().is2xxSuccessful();
        } catch (Exception e) {
            log.error("检查流是否存在失败: {}", e.getMessage());
            return false;
        }
    }
    
    @Override
    public String getStreamInfo(String streamName) {
        try {
            String url = srsServerUrl + srsApiPath + "/streams/" + streamName;
            ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
            return response.getBody();
        } catch (Exception e) {
            log.error("获取流信息失败: {}", e.getMessage());
            return null;
        }
    }
    
    @Override
    public boolean enableDvr(String streamName) {
        try {
            // 通过SRS API启用DVR录制
            // SRS的DVR录制通常通过配置文件启用，但也可以通过API动态控制
            String url = srsServerUrl + srsApiPath + "/raw?rpc=enable_dvr&stream=" + streamName;
            ResponseEntity<Map> response = restTemplate.postForEntity(url, null, Map.class);
            if (response.getStatusCode().is2xxSuccessful()) {
                Map<String, Object> body = response.getBody();
                if (body != null && (Integer) body.getOrDefault("code", -1) == 0) {
                    log.info("DVR录制已启用: {}", streamName);
                    return true;
                }
            }
            log.warn("启用DVR录制失败: {}", streamName);
            return false;
        } catch (Exception e) {
            log.error("启用DVR录制异常: {}", e.getMessage());
            return false;
        }
    }
    
    @Override
    public boolean disableDvr(String streamName) {
        try {
            String url = srsServerUrl + srsApiPath + "/raw?rpc=disable_dvr&stream=" + streamName;
            ResponseEntity<Map> response = restTemplate.postForEntity(url, null, Map.class);
            if (response.getStatusCode().is2xxSuccessful()) {
                Map<String, Object> body = response.getBody();
                if (body != null && (Integer) body.getOrDefault("code", -1) == 0) {
                    log.info("DVR录制已禁用: {}", streamName);
                    return true;
                }
            }
            log.warn("禁用DVR录制失败: {}", streamName);
            return false;
        } catch (Exception e) {
            log.error("禁用DVR录制异常: {}", e.getMessage());
            return false;
        }
    }
}
